﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for SQLFiles
/// </summary>
public class SQLFiles
{
    public SQLFiles()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public String DataSetName(string psSQLFiles,string psSubset)
    {

        string[] saSQLFiles = psSQLFiles.Split(',');
        if (saSQLFiles.Length == 1) saSQLFiles = psSQLFiles.Split('+');
        string sDatasetName = saSQLFiles[0]; //+ (psSubset.Length > 0 ? "_" + psSubset : "");
        return sDatasetName;
    }

    public static string ConstructSQL(string psSQLFiles,string psSubset)
    {
        string[] saSQLFiles = psSQLFiles.Split(',');
        if (saSQLFiles.Length == 1) saSQLFiles = psSQLFiles.Split('+');
        //string sDatasetName = saSQLFiles[0] + (psSubset.Length > 0 ? "_" + psSubset : "");
        
        String sSQL = string.Empty;
        if (psSQLFiles.Split('+').Length == 1)
            for (int i = 0; i < saSQLFiles.Length; i++)
                sSQL += System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath(string.Format("SQL\\{0}.sql", saSQLFiles[i])));
        else
        {
            string sTemplate = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath(string.Format("SQL\\{0}.sql", saSQLFiles[0])));
            string sDetails = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath(string.Format("SQL\\{0}.sql", saSQLFiles[1])));
            if (saSQLFiles.Length > 2)
            {
                string sDetails2 = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath(string.Format("SQL\\{0}.sql", saSQLFiles[2])));
                string sDetails3 = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath(string.Format("SQL\\{0}.sql", saSQLFiles[3])));
                sSQL = String.Format(sTemplate, sDetails, sDetails2, sDetails3);
            }
            else
                sSQL = String.Format(sTemplate, sDetails);
        }

        if (psSubset.Length > 0) sSQL = string.Format(sSQL, psSubset);

        return sSQL;
    }

    public static string ConstructReportsSQL(string psSQLFiles, string psSubset)
    {
        if (psSQLFiles == null) return "" ;
        string[] saSQLFiles = psSQLFiles.Split(',');
        if (saSQLFiles.Length == 1) saSQLFiles = psSQLFiles.Split('+');
        //string sDatasetName = saSQLFiles[0] + (psSubset.Length > 0 ? "_" + psSubset : "");

        String sSQL = string.Empty;
        if (psSQLFiles.Split('+').Length == 1)
            for (int i = 0; i < saSQLFiles.Length; i++)
                sSQL += System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath(string.Format("reports\\SQL\\{0}.sql", saSQLFiles[i])));
        else
        {
            string sTemplate = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath(string.Format("reports\\SQL\\{0}.sql", saSQLFiles[0])));
            string sDetails = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath(string.Format("reports\\SQL\\{0}.sql", saSQLFiles[1])));
            if (saSQLFiles.Length > 2)
            {
                string sDetails2 = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath(string.Format("reports\\SQL\\{0}.sql", saSQLFiles[2])));
                string sDetails3 = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath(string.Format("reports\\SQL\\{0}.sql", saSQLFiles[3])));
                sSQL = String.Format(sTemplate, sDetails, sDetails2, sDetails3);
            }
            else
                sSQL = String.Format(sTemplate, sDetails);
        }

        if (psSubset.Length > 0) sSQL = string.Format(sSQL, psSubset);

        return sSQL;
    }

    public static void WriteTextFile(string psPath,  string psText)
    {
        System.IO.File.WriteAllText(psPath, psText);
    }

    public static string ReadTextFile(string psPath)
    {
      string sText =  System.IO.File.ReadAllText(psPath);
      return sText;
    }

}


